package MyTripBusServices;


import java.awt.Color;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.*;

import javax.swing.*;
public class usermodel extends JFrame implements ActionListener{
	JPanel picture,logarea,titlePic;
	JLabel logTitle,logname,logpwd;
	JTextField tname,tpwd;
	JButton sign,sub;
	usermodel(){
		titlePic=new JPanel();
		titlePic.setBounds(0, 0, 800, 150);
		titlePic.setBorder(BorderFactory.createEtchedBorder());
		titlePic.setLayout(null);
		add(titlePic);
		
		
		picture=new JPanel();
		picture.setBounds(0, 150, 400, 400);
		picture.setBackground(Color.white);
		picture.setBorder(BorderFactory.createEtchedBorder());
		picture.setLayout(null);
		add(picture);
		
		logarea=new JPanel();
		logarea.setBounds(400,150,400,400);
		logarea.setBackground(Color.white);
		logarea.setBorder(BorderFactory.createEtchedBorder());
		logarea.setLayout(null);
		add(logarea);
		
		//picture thing
		JLabel picRoad,picTitle;
		
		picRoad=new JLabel("");
		picRoad.setBounds(0, 0, 400, 400);
		ImageIcon icon = new ImageIcon(this.getClass().getResource("/bus.jpg"));
		Image img = icon.getImage().getScaledInstance(picRoad.getWidth(),picRoad.getHeight(),1);
	    ImageIcon icon1 = new ImageIcon(img);
	    picRoad.setIcon(icon1);
	    picture.add(picRoad);
	    
	    picTitle=new JLabel("");
	    picTitle.setBounds(0,0,800,150);
	    ImageIcon icon2 = new ImageIcon(this.getClass().getResource("/title2.png"));
	    Image img2 = icon2.getImage().getScaledInstance(picTitle.getWidth(),picTitle.getHeight(),1);
	    ImageIcon icon3 = new ImageIcon(img2);
	    picTitle.setIcon(icon3);
	    titlePic.add(picTitle);
	    
		//logarea things
		logTitle=new JLabel("Members Login");
		logTitle.setFont(new Font("Arial",Font.BOLD,25));
		logTitle.setForeground(Color.magenta);
		logTitle.setBounds(100,-200,200,500);
		logarea.add(logTitle);
		
		logname=new JLabel("Username");
		logname.setFont(new Font("Calibri",Font.BOLD,20));
		logname.setBounds(5,70,100,100);
		logarea.add(logname);

		tname=new JTextField("Enter name..");
		tname.setFont(new Font("Calibri",Font.ITALIC,18));
		tname.setBounds(100, 100, 200, 30);
		logarea.add(tname);
		
		logpwd=new JLabel("Password");
		logpwd.setBounds(5,120,100,100);
		logpwd.setFont(new Font("Calibri",Font.BOLD,20));
		logarea.add(logpwd);
		
		tpwd=new JPasswordField();
		tpwd.setFont(new Font("Calibri",Font.ITALIC,18));
		tpwd.setBounds(100, 150, 200, 30);
		logarea.add(tpwd);
		
		//buttons
		sign=new JButton("Sign up");
		sign.setFont(new Font("Calibri",Font.BOLD,15));
		sign.setBounds(100, 200, 80, 22);
		sign.setBackground(Color.orange);
		logarea.add(sign);
		
		sub=new JButton("Submit");
		sub.setFont(new Font("Calibri",Font.BOLD,15));
		sub.setBounds(200, 200, 80, 22);
		sub.setBackground(Color.yellow);
		logarea.add(sub);
		
		setLayout(null);
		setSize(800, 600);
		setTitle("My Bus Travel Service");
		setVisible(true);
		
		
		sign.addActionListener(this);
		sub.addActionListener(this);
	}
	@Override
	public void actionPerformed(ActionEvent e) {

		if(e.getActionCommand()=="Sign up") {
			Registration rs=new Registration();
			this.dispose();
		}
		if(e.getActionCommand()=="Submit") {
			checklog();
			this.dispose();
		}
	}
	public static Connection createConnection() {
		Connection con=null;
     try {
			Class.forName("com.mysql.cj.jdbc.Driver");
			con=DriverManager.getConnection(
					"jdbc:mysql://localhost:3306/project?serverTimezone=UTC","root","131026");
		}
		catch(Exception e) {
			System.out.print(e);
		}
		return con;
		
	}
	
	public void checklog(){
		try{
			Connection con=createConnection();
        	String str = "select *from users where user_name='"+tname.getText()+"' and user_pwd='"+tpwd.getText()+"'";
        	PreparedStatement stmt=con.prepareStatement(str); 
        	ResultSet rs=stmt.executeQuery();
        	if(rs.next()){
        		JOptionPane.showMessageDialog(this,"you have logged in"); 
        		BookTickets.currentname=tname.getText();
        		BookTickets bk=new BookTickets(rs.getInt("id"));
        	}
        	else {
        		JOptionPane.showMessageDialog(this,"User name do not exists"); 
        	}
    	}
    	catch (Exception e){
    		System.out.println(e);
    	}   
	}

	public static void main(String[] args) {
		// TODO Auto-generated method stub
		new usermodel();
	}
	
}
